import { Request, Response } from "express";
import { EventType } from "../db";
import { checkAuthorization, getUserId } from "./util";

const list = [
  {
    name: "日常生活",
    icon: "daily",
    color: "#CB75F7",
    items: [
      { name: "早午晚餐", icon: "eat", color: "#ff9c6e" },
      { name: "休息睡觉", icon: "sleep", color: "#ffc069" },
      { name: "行车交通", icon: "traffic", color: "#fff566" },
      { name: "上班工作", icon: "tool", color: "#ffd666" },
      { name: "沐浴泡澡", icon: "lave", color: "#95de64" },
      { name: "洗漱护肤", icon: "wash", color: "#5cdbd3" },
      { name: "清洁扫除", icon: "clean", color: "#85a5ff" },
      { name: "洗菜做饭", icon: "kitchen", color: "#ff85c0" },
      { name: "私事处理", icon: "apparel", color: "#40a9ff" },
      { name: "晨前事务", icon: "leisure", color: "#d3f261" },
      { name: "日间小憩", icon: "music", color: "#b37feb" },
    ],
  },
  {
    name: "个人修行",
    icon: "practice",
    color: "#B35C44",
    items: [
      { name: "听书阅读", icon: "read", color: "#52c41a" },
      { name: "运动健身", icon: "motion", color: "#13c2c2" },
      { name: "修心坐定", icon: "meditation", color: "#fadb14" },
      { name: "个人开发", icon: "dev", color: "#096dd9" },
    ],
  },
  {
    name: "休闲娱乐",
    icon: "leisure",
    color: "#7DCDA4",
    items: [
      { name: "腐败聚会", icon: "meet", color: "#faad14" },
      { name: "休闲玩乐", icon: "game", color: "#d4b106" },
      { name: "旅游度假", icon: "travel", color: "#7cb305" },
      { name: "小说短篇", icon: "read", color: "#d46b08" },
      { name: "煲剧观影", icon: "movie", color: "#d4380d" },
    ],
  },
  {
    name: "人情来往",
    icon: "gift",
    color: "#83BBF9",
    items: [{ name: "聊天沟通", icon: "icq", color: "#722ed1" }],
  },
  {
    name: "其他杂项",
    icon: "other",
    color: "#ECAA57",
    items: [{ name: "烂账处理", icon: "baddebt", color: "#c41d7f" }],
  },
];

export async function create(UserId: string) {
  let index = 0;
  for (const { items, ...rest } of list) {
    index++;
    const item = await EventType.create({ ...rest, index, UserId });
    const parentId = item.get("id");
    for (const iterator of items) {
      index++;
      await EventType.create({ ...iterator, index, parentId, UserId });
    }
  }
}

export default {
  "GET /api/event-type/list": async (req: Request, res: Response) => {
    if (checkAuthorization(req, res)) return;
    const UserId = getUserId(req, res);
    if (!UserId) return;
    const { parentId = null } = req.query as any;
    const total = await EventType.count({ where: { UserId } });
    if (!total) {
      await create(UserId);
    }
    const rows = await EventType.findAll({
      where: { parentId, UserId },
      order: ["index"],
      attributes: ["id", "name", "icon","color"]
    });
    res.send({
      success: true,
      total: rows.length,
      data: rows,
    });
  },
  // 修改某个type
  "PUT /api/event-type": async (req: Request, res: Response) => {
    if (checkAuthorization(req, res)) return;
    const { id, name, icon, color, parentId } = req.body;
    const log = await EventType.findOne({ where: { id } });
    if (!log) {
      res.send({
        success: false,
        data: null,
        errorCode: "710",
        errorMessage: "该类型不存在！",
      });
      return;
    }
    await EventType.update({ name, icon, color, parentId }, { where: { id } });
    res.send({
      success: true,
      data: id,
    });
  },
  // 新增一个type
  "POST /api/event-type": async (req: Request, res: Response) => {
    if (checkAuthorization(req, res)) return;
    const UserId = getUserId(req, res);
    if (!UserId) return;
    const { name, icon, color, parentId } = req.body;
    console.log("POST /api/event-log ", req.body);
    const log = await EventType.create({ name, icon, color, parentId, UserId });
    res.send({
      success: true,
      data: log.get("id"),
    });
  },
};
